/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pass.jtorrenteditor.list.model;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import za.co.pass.jtorrenteditor.helper.Utils;
import za.co.pass.jtorrenteditor.list.model.data.AnnounceData;

/**
 *
 * @author alabuschagne
 */
public class AnnounceListModel implements ListModel, ListCellRenderer
{
    private List list = new LinkedList();
    private List<AnnounceData> listExtra = new LinkedList<AnnounceData>();

    public int getSize()
    {
        return list.size();
    }

    public Object getElementAt(int index)
    {
        if(list.get(index) instanceof List)
        {
            if(((List) list.get(index)).get(0) instanceof short[])
            {
                return new String(Utils.short2Char((short[]) ((List) list.get(index)).get(0)));
            }
            else if(((List) list.get(index)).get(0) instanceof String)
            {
                return (String) ((List) list.get(index)).get(0);
            }
            else
            {
                return null;
            }
        }
        else if(list.get(index) instanceof short[])
        {
            return new String(Utils.short2Char((short[]) list.get(index)));
        }
        else if(list.get(index) instanceof String)
        {
            return (String) list.get(index);
        }
        else
        {
            return null;
        }
    }

    public void addListDataListener(ListDataListener l)
    {
    }

    public void removeListDataListener(ListDataListener l)
    {
    }

    /**
     * @return the list
     */
    public List getList()
    {
        return list;
    }

    public List<AnnounceData> getListExtra()
    {
        return listExtra;
    }

    /**
     * @param list the list to set
     */
    public void setList(List list)
    {
        this.list = list;
        listExtra = new LinkedList<AnnounceData>();
        for(int i = 0; i < this.list.size(); i++)
        {
            listExtra.add(new AnnounceData((List)this.list.get(i)));
        }
    }

    public void updateExtra(String url, boolean busy, boolean checked, boolean ok)
    {
        ListIterator<AnnounceData> it = listExtra.listIterator();
        while(it.hasNext())
        {
            AnnounceData a = it.next();
            if(a.getUrl().compareToIgnoreCase(url) == 0)
            {
                a.setBusy(busy);
                a.setChecked(checked);
                a.setOk(ok);
                return;
            }
        }
    }

    public boolean add(String turl)
    {
        //check if it is in the list
        for(int i = 0; i < getSize(); i++)
        {
            String str = (String) getElementAt(i);
            if(str.compareToIgnoreCase(turl) == 0)
            {
                return false;
            }
        }
        List l = new LinkedList();
        l.add(Utils.byte2Short(turl.getBytes()));
        list.add(l);
        listExtra.add(new AnnounceData(turl));
        return true;
    }

    public void del(int x) throws IndexOutOfBoundsException
    {
        list.remove(x);
        listExtra.remove(x);
    }

    public void set(int x, String cturl)
    {
        List l = new LinkedList();
        l.add(Utils.byte2Short(cturl.getBytes()));
        list.set(x, l);
        AnnounceData ad = listExtra.get(x);
        ad.setUrl(cturl);
    }

    public boolean up(int x)
    {
        if(x > 0)
        {
            List l1 = (List) list.get(x);
            List l2 = (List) list.get(x - 1);
            list.set(x, l2);
            list.set(x - 1, l1);
            AnnounceData a1 = listExtra.get(x);
            AnnounceData a2 = listExtra.get(x - 1);
            listExtra.set(x, a2);
            listExtra.set(x - 1, a1);
            return true;
        }
        return false;
    }

    public boolean down(int x)
    {
        if(x < list.size() - 1)
        {
            List l1 = (List) list.get(x);
            List l2 = (List) list.get(x + 1);
            list.set(x, l2);
            list.set(x + 1, l1);
            AnnounceData a1 = listExtra.get(x);
            AnnounceData a2 = listExtra.get(x + 1);
            listExtra.set(x, a2);
            listExtra.set(x + 1, a1);
            return true;
        }
        return false;
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        JLabel label = new JLabel();
        label.setOpaque(true);
        Font font = label.getFont();
        label.setFont(font.deriveFont(Font.PLAIN));

        if(isSelected)
        {
            label.setFont(font.deriveFont(Font.BOLD));
        }
        label.setText((String) value);
        AnnounceData a = listExtra.get(index);
        if((a.getUrl() == null) || (a.getUrl().length() == 0))
        {
            a.setUrl((String) value);
            listExtra.set(index, a);
        }
        //c b o C
        //0 0 0 White
        //0 1 0 Yellow
        //1 0 0 Red
        //1 0 1 Green
        //rest is invalid
        if((!a.isChecked()) && (!a.isBusy()) && (!a.isOk()))
        {
            label.setBackground(new Color(0xFFFFFF));
        }
        else if((!a.isChecked()) && (a.isBusy()) && (!a.isOk()))
        {
            label.setBackground(new Color(0xFFFF00));
        }
        else if((a.isChecked()) && (!a.isBusy()) && (!a.isOk()))
        {
            label.setBackground(new Color(0xFF0000));
        }
        else if((a.isChecked()) && (!a.isBusy()) && (a.isOk()))
        {
            label.setBackground(new Color(0x00FF00));
        }
        label.setText("[" + Integer.toString(a.getNumberOfSeeds()) + "]" + label.getText());
        return label;
    }

    public AnnounceData getAnnounceData(int i)
    {
        return listExtra.get(i);
    }

    public synchronized void setAnnounceData(int i, AnnounceData ad)
    {
        if((i >= 0) && (i < listExtra.size()))
        {
            listExtra.set(i, ad);
        }
    }
}
